package com.cloud.school.bus.teacherhelper.base.database;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.cloud.school.bus.teacherhelper.entitys.Picture;

/**
 * 所有照片,忽略
 * 
 * @author hongfeijia
 * 
 */
public class AllPictureIgnoreDB extends SQLiteOpenHelper {

	/** 数据库名 */
	public static final String DB_NAME = "allPictureIgnore.db";
	/** 表名 */
	public static final String TABLE_NAME = "allPictureIgnore";

	public static final String PICTURE_PATH = "picturePath";

	public static final String IS_IGNORE = "isIgnore";

	private AllPictureIgnoreDB(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	public static AllPictureIgnoreDB getAllPictureIgnoreDB(Context context) {
		return new AllPictureIgnoreDB(context, DB_NAME, null, 1);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String execSQLString = "create table if not exists " + TABLE_NAME
				+ " (" + PICTURE_PATH + " varchar," + IS_IGNORE + " varchar)";

		db.execSQL(execSQLString);
		Log.e("execSQLString::::::", execSQLString);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	public void putInsert(Picture picture) {
		if (!isExists(picture)) {
			SQLiteDatabase sqLiteDatabase = getWritableDatabase();
			String sqlString = "insert into " + TABLE_NAME + " values ('"
					+ picture.getPicturePath() + "','" + picture.isSelected
					+ "')";
			Log.e("sqlString insert ::::::", sqlString);
			sqLiteDatabase.execSQL(sqlString);
			sqLiteDatabase.close();

		} else {
			SQLiteDatabase sqLiteDatabase = getWritableDatabase();
			String sqlString = "update " + TABLE_NAME + " set " + IS_IGNORE
					+ "=" + (picture.isSelected + "") + " where "
					+ PICTURE_PATH + "=" + picture.getPicturePath();
			sqLiteDatabase.execSQL(sqlString);
			Log.e("sqlString update ::::::", sqlString);
			sqLiteDatabase.close();
		}

	}

	public void putInsert(List<Picture> pictures) {
		SQLiteDatabase sqLiteDatabase = getWritableDatabase();
		sqLiteDatabase.beginTransaction();
		try {
			for (int i = 0; i < pictures.size(); i++) {
				Picture picture = pictures.get(i);

				boolean flag = false;
				Cursor cursor = sqLiteDatabase.rawQuery("select * from "
						+ TABLE_NAME + " where " + PICTURE_PATH + "=?",
						new String[] { picture.getPicturePath() });
				flag = cursor.getCount() == 0 ? false : true;
				cursor.close();
				if (!flag) {
					String sqlString = "insert into " + TABLE_NAME
							+ " values ('" + picture.getPicturePath() + "','"
							+ picture.isSelected + "')";
					Log.e("sqlString::::::", sqlString);
					sqLiteDatabase.execSQL(sqlString);

				} else {
					String sqlString = "update " + TABLE_NAME + " set "
							+ IS_IGNORE + "='" + (picture.isSelected + "")
							+ "' where " + PICTURE_PATH + "='"
							+ picture.getPicturePath() + "'";
					Log.e("sqlString::::::", sqlString);
					sqLiteDatabase.execSQL(sqlString);
				}

			}
			sqLiteDatabase.setTransactionSuccessful();
		} finally {
			sqLiteDatabase.endTransaction();
		}

		sqLiteDatabase.close();
	}

	public void remove(Picture picture) {
		if (isExists(picture)) {
			SQLiteDatabase sqLiteDatabase = getWritableDatabase();
			sqLiteDatabase.execSQL("delete from " + TABLE_NAME + " where "
					+ PICTURE_PATH + "='" + picture.getPicturePath() + "'");
			sqLiteDatabase.close();
		}
	}

	public boolean isExists(Picture picture) {
		SQLiteDatabase sqLiteDatabase = getWritableDatabase();

		boolean flag = false;
		Cursor cursor = sqLiteDatabase.rawQuery("select * from " + TABLE_NAME
				+ " where " + PICTURE_PATH + "=?",
				new String[] { picture.getPicturePath() });
		flag = cursor.getCount() == 0 ? false : true;
		cursor.close();

		sqLiteDatabase.close();
		return flag;
	}

	public List<Picture> getPictureList() {
		SQLiteDatabase sqLiteDatabase = getWritableDatabase();

		List<Picture> articles = new ArrayList<Picture>();
		Cursor cursor = sqLiteDatabase.rawQuery("select * from " + TABLE_NAME,
				null);
		cursor.moveToFirst();
		for (int i = 0; i < cursor.getCount(); i++) {
			Picture picture = new Picture();
			// picture.picturePath = cursor.getString(0);
			picture.setPicturePath(cursor.getString(0));
			String ignore = cursor.getString(1);
			if ("true".equals(ignore)) {
				picture.isIgnore = true;
			} else if ("false".equals(ignore)) {
				picture.isIgnore = false;
			}

			articles.add(picture);

			cursor.moveToNext();
		}
		cursor.close();

		sqLiteDatabase.close();

		return articles;
	}
}
